<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends Controller {
    public function register(){
       $this->display();
    }


    public function ajaxRegister(){
    	 if(!empty($_POST)){
               $userModel = D('User');
            if($userModel->create()){
                 if($user_id=$userModel->add()){
                        $arr=array(
                              'code'   =>1,
                              'content'=>'注册成功'
                            );
                 }               
            }else{
                 $error=$userModel->getError();
                 $arr=array(
                              'code'   =>0,
                              'content'=>$error
                            );
            }
       }else{
             $error=$userModel->getError();
             $arr=array(
                         'code'   =>0,
                         'content'=>$error
                       );
       }

      return $this->ajaxReturn($arr);
    }
    public function verify(){
         $config =    array(    
         'fontSize'    =>    50,    // 验证码字体大小   
         'length'      =>    6,     // 验证码位数    
         'useNoise'    =>    false, // 关闭验证码杂点
         );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }
    public function login(){
       if($_POST){
        $arr=array(
           'username'=>I('post.name'),
           'password'=>md5(I('post.pass'))
          );        
           $userModel=D('User');
           $result=$userModel->checkLogin($arr);
           if($result){
                 session('user',array(
                  'username'=>I('post.name'),
                  'user_id' =>$result[0]['id'],
                  'email'   =>$result[0]['email']
                  ));
                 $this->success("登陆成功",U('Index/index'));
           }else{
                 $this->display();
           }
       }else{
         $this->display();
       }
      
    }
    public function logout(){
         session('user',null);
         $this->redirect('Index/index');
    }
    public function person(){

         $this->display();
    }
     public function userInfo(){
      if(IS_POST){
         $id=$_SESSION['user']['user_id'];
         $data=$_POST;
         $userModel=D('User');
         if($userModel->updateUserInfo($id,$data)){
            $this->redirect('User/person');
         }
      }
         
         $this->display();
    }   
     public function uploadhead(){
       $id=$_SESSION['user']['user_id'];
       if(IS_POST){
            if($_FILES['file']['error']==0){
                  $upload= new \Think\Upload();
                 $upload->maxSize= 3145728 ;  // 设置附件上传大小 
                 $upload->exts=array(
                    'jpg','png','gif','jpej'
                  );
                 $upload->rootPath  =  'Public';
                 $upload->savePath  =  './Uploads/';
                 $info   =   $upload->upload();        
                  if(!$info) {   // 上传错误提示错误信息        
                     $this->error($upload->getError());    
                  }else{    // 上传成功  
                    $path =$info ['file'] ['savepath'].$info ['file'] ['savename'];
                    $path=substr($path,1);
                    $userModel=D('User');
                                   
                    if($userModel->uploadHead($id,$path)){
                         $this->redirect('User/uploadhead');
                    }
                  }
            }else{
                 var_dump($_POST);
                 die;
            }
            
        }
        $userModel=D('User');
        $result=$userModel->where('id='.$id)
                  ->field('head_img')
                  ->select();
        if($result){
            $this->assign('images',$result[0]);
        }
        $this->display();
    }
    public function addressAjax(){   
         if($_POST['send']=="province"){

            $addressModel=D('provinces');
            $mr=$addressModel->field('provinceid,province')
                             ->select();            
          }elseif($_POST['send']=="city"){
            $id=$_POST['id'];
            $addressModel=D('cities');
            $where=array('provinceid'=>$id);
            $mr=$addressModel->where($where)
                             ->field('cityid,city')
                             ->select();
          }elseif($_POST['send']=="area"){
            $id=$_POST['id'];
            $addressModel=D('areas');
            $where=array('cityid'=>$id);
            $mr=$addressModel->where($where)
                             ->field('areaid,area')
                             ->select();     
           }
           return $this->ajaxReturn($mr);
      }
    public function address(){ 
         $_POST['user_id']=($_SESSION['user']['user_id']);
         if(IS_POST){
               $addressModel=M('address');
               $where['user_id']=$_POST['user_id'];
               $where['isdefault']=1;
               $result=$addressModel->where($where)
                            ->find();
               if(!$result){
                  $_POST['isdefault']=1;
               }
               $addressModel->create();
               if($addressModel->add()){
                    $this->redirect('User/address');
               }                             
         }
         $address=D('address');
         $allAddress=$address->allAddress( $_POST['user_id']);
         $this->assign('address',$allAddress);
         $this->display();
    }
   public function deleteAddress(){ 
        $id=I('get.id');
        $addressModel=D('address');
        $result=$addressModel->deleteAdd($id);
        if($result){
            $this->success('删除成功',U('User/address'));
        }else{
           
        }
   }
   public function updateDefault(){              
        $data['user_id']= $_SESSION['user']['user_id'];
        $data['id']= $id=I('get.id');
        $addressModel=D('address'); 
        if($result=$addressModel->updateDe($data)){
            $this->redirect('User/address');
        }else{
            $this->error('设为默认失败');
        } 
   }

    public function edit() {
         if(IS_POST){     
              $user_id=($_SESSION['user']['user_id']);
              $id=I('post.id');
              $addressModel=D('address');
              $result=$addressModel->allAddress($user_id,$id);
              if($result){
                  return $this->ajaxReturn($result);
              }
         }
    }
    public function upAddress() {
         if(IS_POST){
            $id=$_POST['id'];
            $data=array(
               'receive_person'=>$_POST['receive_person'],
               'province_id'   =>$_POST['province_id'],
               'city_id'       =>$_POST['city_id'],
               'area_id'       =>$_POST['area_id'],
               'detail_address'=>$_POST['detail_address'],
               'receive_mobile'=>$_POST['receive_mobile'],
            );
            $addressModel=D('address');
            $result=$addressModel->updateAddress($id,$data);
            if(!$result===false){
                $this->redirect('User/address');
            }
            $this->redirect('User/address');
         }
          
    }
    public function  changepass(){
          $mail=$_SESSION['user']['email'];
          $str=substr_replace($mail,'****',3,4);        
          $this->assign('email',$str);
          $this->display();
    }
    public function email_varify(){
       if(IS_POST){   
        $mail=$_SESSION['user']['email'];    
        $rand=rand(100000,999999);
        session('verify',array('code'=>$rand,'expire'=>60));
        $aa = sendMail($mail,'汇一家',$rand);
        $this->assign('verify',$_SESSION['verify']['code']);
        return $this->ajaxReturn($_SESSION['verify']['code']);
        // $_SESSION['time'] = time();
        // 2min后
        if ((time() - $_SESSION['time']) > 20) {
            //失效了
            unset($_SESSION['verify']);
        }
       }    
    }
     public function updatePassword(){
       if(IS_POST){  
          $userModel=D('User');      
          $result=$userModel->check_verify($_POST['varify_2']);
          if($result){
               $this->display();
          }
       }    
    }
}